package pers.rain.dynamiccompiledemo.schedule;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import pers.rain.dynamiccompiledemo.holder.ConfigHolder;
import pers.rain.dynamiccompiledemo.model.Config;
import pers.rain.dynamiccompiledemo.plugin.IPlugin;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Component
@EnableScheduling
@Slf4j
public class DemoSchedule {
    @Resource
    private ConfigHolder configHolder;

    @Scheduled(cron = "0/5 * * * * ?")
    public void execute() {
        Config config = configHolder.getConfig();
        if (Objects.nonNull(config)) {
            List<Config.ConfigItem> classList = config.getClassList();
            if (CollUtil.isNotEmpty(classList)) {
                for (Config.ConfigItem configItem : classList) {
                    IPlugin iPlugin = configItem.getIPlugin();
                    if (Objects.nonNull(iPlugin)) {
                        log.info(iPlugin.getContent());
                    }
                }
            }
        }
    }
}
